package com.android.jblm.commandcentral;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.android.jblm.commandcentral.adapters.HeaderAdapter;
import com.android.jblm.commandcentral.objects.Category;
import com.android.jblm.commandcentral.objects.Contact;

import java.util.HashMap;

/**
 * Created by Brian on 8/20/2014.
 */
public class ContactListFragment extends ListFragment {

    OnContactSelectedListener mCallBack;
    Contact[] contacts;

    // Container Activity must implement this interface
    public interface OnContactSelectedListener {
        public void onContactSelected(Contact c);
        public int getSelectedCategoryPos();
        public Category getCurCategory();
        public HashMap<String, Contact[]> getMyContacts();
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        contacts = mCallBack.getMyContacts()
                .get(mCallBack.getCurCategory().getTitle());

        Log.w("SubHeaderFragment", "Header List Created");
        // Populate list with our static array of myHeaders.
        setListAdapter(new HeaderAdapter(getActivity(), contacts));
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mCallBack = (OnContactSelectedListener) activity;



        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeaderSelectedListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallBack = null;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        mCallBack.onContactSelected(contacts[position]);



        // Set the item as checked to be highlighted when in two-pane layout
        getListView().setItemChecked(position, true);

    }

}
